[RequireComponent(typeof(AIMovement))]
public class FieldOfView : MonoBehaviour
{
private AIMovement AI;
[HideInInspector] public float ViewRadius;
[HideInInspector] public float ViewAngle;
private LayerMask TargetMask;
private LayerMask ObstacleMask;
[HideInInspector]
public List<Transform> VisibleTargets = new List<Transform>();
[SerializeField] private float MeshResolution;
[SerializeField] private int EdgeResolveIterations;
[SerializeField] private float EdgeDstThreshold;
public MeshFilter ViewMeshFilter;
Mesh viewMesh;
public Vector3 TargetPos;
private void Awake()
{
AI = GetComponent<AIMovement>();
}
void Start()
{
viewMesh = new Mesh();
viewMesh.name = "View Mesh";
ViewMeshFilter.mesh = viewMesh;
StartCoroutine("FindTargetsWithDelay", .2f);
}
private void FixedUpdate()
{
SetSettings();
}
IEnumerator FindTargetsWithDelay(float delay)
{
while (true)
{
yield return new WaitForSeconds(delay);
FindVisibleTargets();
}
}
void LateUpdate()
{
DrawFieldOfView();
}
private void SetSettings()
{
if (ViewRadius != AI.AIStats.ViewRadius)
ViewRadius = AI.AIStats.ViewRadius;
if (ViewAngle != AI.AIStats.ViewAngle)
ViewAngle = AI.AIStats.ViewAngle;
if (TargetMask != AI.AIStats.LookForLayer)
TargetMask = AI.AIStats.LookForLayer;
if (ObstacleMask != AI.AIStats.ObstacleLayer)
ObstacleMask = AI.AIStats.ObstacleLayer;
}
private void FindVisibleTargets()
{
VisibleTargets.Clear();
Collider[] targetsInViewRadius = Physics.OverlapSphere(transform.position, ViewRadius, TargetMask);
for (int i = 0; i < targetsInViewRadius.Length; i++)
{
Transform target = targetsInViewRadius[i].transform;
Vector3 dirToTarget = (target.position - transform.position).normalized;
if (Vector3.Angle(transform.forward, dirToTarget) < ViewAngle / 2)
{
float dstToTarget = Vector3.Distance(transform.position, target.position);
if (!Physics.Raycast(transform.position, dirToTarget, dstToTarget, ObstacleMask))
{
VisibleTargets.Add(target);
AI.GoToPos = new Vector3(target.position.x, target.position.y, target.position.z);
AI.state = AgentState.attack;
}
}
}
}
void DrawFieldOfView()
{
int stepCount = Mathf.RoundToInt(ViewAngle * MeshResolution);
float stepAngleSize = ViewAngle / stepCount;
List<Vector3> viewPoints = new List<Vector3>();
ViewCastInfo oldViewCast = new ViewCastInfo();
for (int i = 0; i <= stepCount; i++)
{
float angle = transform.eulerAngles.y - ViewAngle / 2 + stepAngleSize * i;
ViewCastInfo newViewCast = ViewCast(angle);
if (i > 0)
{
bool edgeDstThresholdExceeded = Mathf.Abs(oldViewCast.dst - newViewCast.dst) > EdgeDstThreshold;
if (oldViewCast.hit != newViewCast.hit || (oldViewCast.hit && newViewCast.hit && edgeDstThresholdExceeded))
{
EdgeInfo edge = FindEdge(oldViewCast, newViewCast);
if (edge.pointA != Vector3.zero)
{
viewPoints.Add(edge.pointA);
}
if (edge.pointB != Vector3.zero)
{
viewPoints.Add(edge.pointB);
}
}
}
viewPoints.Add(newViewCast.point);
oldViewCast = newViewCast;
}
int vertexCount = viewPoints.Count + 1;
Vector3[] vertices = new Vector3[vertexCount];
int[] triangles = new int[(vertexCount - 2) * 3];
vertices[0] = Vector3.zero;
for (int i = 0; i < vertexCount - 1; i++)
{
vertices[i + 1] = transform.InverseTransformPoint(viewPoints[i]);
if (i < vertexCount - 2)
{
triangles[i * 3] = 0;
triangles[i * 3 + 1] = i + 1;
triangles[i * 3 + 2] = i + 2;
}
}
viewMesh.Clear();
viewMesh.vertices = vertices;
viewMesh.triangles = triangles;
viewMesh.RecalculateNormals();
}
EdgeInfo FindEdge(ViewCastInfo minViewCast, ViewCastInfo maxViewCast)
{
float minAngle = minViewCast.angle;
float maxAngle = maxViewCast.angle;
Vector3 minPoint = Vector3.zero;
Vector3 maxPoint = Vector3.zero;
for (int i = 0; i < EdgeResolveIterations; i++)
{
float angle = (minAngle + maxAngle) / 2;
ViewCastInfo newViewCast = ViewCast(angle);
bool edgeDstThresholdExceeded = Mathf.Abs(minViewCast.dst - newViewCast.dst) > EdgeDstThreshold;
if (newViewCast.hit == minViewCast.hit && !edgeDstThresholdExceeded)
{
minAngle = angle;
minPoint = newViewCast.point;
}
else
{
maxAngle = angle;
maxPoint = newViewCast.point;
}
}
return new EdgeInfo(minPoint, maxPoint);
}
ViewCastInfo ViewCast(float globalAngle)
{
Vector3 dir = DirFromAngle(globalAngle, true);
RaycastHit hit;
if (Physics.Raycast(transform.position, dir, out hit, ViewRadius, ObstacleMask))
{
return new ViewCastInfo(true, hit.point, hit.distance, globalAngle);
}
else
{
return new ViewCastInfo(false, transform.position + dir * ViewRadius, ViewRadius, globalAngle);
}
}
public Vector3 DirFromAngle(float angleInDegrees, bool angleIsGlobal)
{
if (!angleIsGlobal)
{
angleInDegrees += transform.eulerAngles.y;
}
return new Vector3(Mathf.Sin(angleInDegrees * Mathf.Deg2Rad), 0, Mathf.Cos(angleInDegrees * Mathf.Deg2Rad));
}
public struct ViewCastInfo
{
public bool hit;
public Vector3 point;
public float dst;
public float angle;
public ViewCastInfo(bool _hit, Vector3 _point, float _dst, float _angle)
{
hit = _hit;
point = _point;
dst = _dst;
angle = _angle;
}
}
public struct EdgeInfo
{
public Vector3 pointA;
public Vector3 pointB;
public EdgeInfo(Vector3 _pointA, Vector3 _pointB)
{
pointA = _pointA;
pointB = _pointB;
}
}
}
using UnityEditor;
[CustomEditor (typeof (FieldOfView))]
public class FieldOfViewEditor : Editor
{
private void OnSceneGUI()
{
FieldOfView fov = (FieldOfView)target;
Handles.color = Color.white;
Handles.DrawWireArc(fov.transform.position, Vector3.up, Vector3.forward, 360, fov.ViewRadius);
Vector3 viewAngleA = fov.DirFromAngle(-fov.ViewAngle / 2, false);
Vector3 viewAngleB = fov.DirFromAngle(fov.ViewAngle / 2, false);
Handles.DrawLine(fov.transform.position, fov.transform.position + viewAngleA * fov.ViewRadius);
Handles.DrawLine(fov.transform.position, fov.transform.position + viewAngleB * fov.ViewRadius);
Handles.color = Color.red;
foreach (Transform visibleTarget in fov.VisibleTargets)
Handles.DrawLine(fov.transform.position, visibleTarget.position);
}
}